package com.xmwzw.catpu;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

import com.xmwzw.catpu.databinding.ActivityLoginBinding;
import com.xmwzw.catpu.http.BaseRequest;
import com.xmwzw.catpu.http.bean.LoginRespond;
import com.xmwzw.catpu.http.listener.LoginListener;
import com.xmwzw.catpu.util.Constant;
import com.xmwzw.catpu.util.SPFaceUtils;
import com.xmwzw.catpu.viewmodel.LoginViewModel;

import java.lang.ref.WeakReference;

public class LoginActivity extends BaseActivity {
    private static final String TAG = LoginActivity.class.getSimpleName();

    private ActivityLoginBinding loginBinding;
    private LoginViewModel loginViewModel;
    private long firstTime = 0;
    private MyHandler myHandler;


    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loginBinding = getBaseViewBinding();
        loginBinding.setModel(loginViewModel = new LoginViewModel());
        loginBinding.setClick(new OnClick());
        myHandler = new MyHandler(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        long secondTime = System.currentTimeMillis();

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (secondTime - firstTime < 2000) {
                System.exit(0);
            } else {
                Toast.makeText(getApplicationContext(), "再按一次返回键退出", Toast.LENGTH_SHORT).show();
                firstTime = System.currentTimeMillis();
            }

            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    public class OnClick {
        public void login() {
            if (!TextUtils.isEmpty(loginViewModel.getValidateError())) {
                showToast(loginViewModel.getValidateError());
                return;
            }

            loginViewModel.setShowLoadingDialog(true);
            BaseRequest.getInstance().login(loginViewModel.mAccount, loginViewModel.mPassword, new LoginListener() {
                @Override
                public void getRespond(LoginRespond loginRespond) {

                    Log.d(TAG, "getRespond: " + loginRespond);
                    Message message = myHandler.obtainMessage();
                    message.what = 1;
                    message.obj = loginRespond;
                    myHandler.sendMessage(message);

                }
            });


        }

        public void normalLogin() {
            Constant.IS_NORMAL_LOGIN = true;

            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }

        public void register() {
            startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
        }
    }

    private static class MyHandler extends Handler {
        WeakReference<LoginActivity> weakReference;

        public MyHandler(LoginActivity loginActivity) {
            weakReference = new WeakReference<>(loginActivity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    weakReference.get().loginViewModel.setShowLoadingDialog(false);
                    LoginRespond loginRespond = (LoginRespond) msg.obj;
                    if (loginRespond.code != 0) {
                        weakReference.get().showToast("登录失败，请检查用户名密码是否正确");
                    } else {
                        SPFaceUtils.put(weakReference.get(), Constant.KEY_LOGIN, true);
                        SPFaceUtils.put(weakReference.get(), Constant.KEY_LOGIN_TOKEN, loginRespond.token);
                        SPFaceUtils.put(weakReference.get(), Constant.KEY_LOGIN_TIME, System.currentTimeMillis());
                        SPFaceUtils.put(weakReference.get(), Constant.KEY_LOGIN_NAME, weakReference.get().loginViewModel.mAccount);
                        SPFaceUtils.put(weakReference.get(), Constant.KEY_LOGIN_PAW, weakReference.get().loginViewModel.mPassword);
                        Constant.IS_NORMAL_LOGIN = false;
                        weakReference.get().startActivity(new Intent(weakReference.get(), MainActivity.class));
                        weakReference.get().finish();

                    }
                    break;

            }
        }
    }
}
